﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Newtonsoft.Json;
using System.Data.OleDb;

namespace Asoft.Web.Controllers
{
    public class ExceptionFilter : FilterAttribute, IExceptionFilter
    {
        void IExceptionFilter.OnException(ExceptionContext filterContext)
        {
            var exc = filterContext.Exception;
            var msg = exc.Message;
            //-2147467259
            if (exc is OleDbException && ((OleDbException)exc).ErrorCode == -2147467259)
            {
                msg = "记录的关键字在系统中已存在，无法添加，请重新填写.";
            }
            filterContext.RequestContext.HttpContext.Response.StatusCode = 500;
            //filterContext.RequestContext.HttpContext.Response.Write(JsonConvert.SerializeObject(new { status = "error", msg = msg }));
            filterContext.RequestContext.HttpContext.Response.Write(msg);
            filterContext.ExceptionHandled = true;
        }
    }

}